package com.qcq.wecheck.controller;


import com.qcq.wecheck.entity.UserSchedule;
import com.qcq.wecheck.result.CommonResult;
import com.qcq.wecheck.service.UserScheduleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户-考勤表 中间表 前端控制器
 * </p>
 *
 * @author O
 * @since 2018-09-24
 */
@RestController
@RequestMapping("/userSchedule")
public class UserScheduleController {
	@Resource
	private UserScheduleService userScheduleService;

	/**
	 * 用户点击参加考勤活动
	 */
	@PostMapping("/engage")
	public CommonResult engage(@RequestParam("openId") String openId,
	                           @RequestParam("scheduleId") Long scheduleId) {
		UserSchedule userSchedule = new UserSchedule();
		userSchedule
				.setOpenId(openId)
				.setScheduleId(scheduleId);
		try {
			userScheduleService.insert(userSchedule);
			return CommonResult.success();
		} catch (Exception e) {
			return CommonResult.failure("您已参与");
		}
	}

	/**
	 * 判断是否参加考勤活动
	 */
	@GetMapping("/isEngaged")
	public CommonResult isEngaged(@RequestParam("openId") String openId,
	                              @RequestParam("scheduleId") Long scheduleId) {
		return userScheduleService.isEngaged(openId, scheduleId) ?
				CommonResult.success("该用户已参与") :
				CommonResult.failure("该用户没有参与");
	}

}

